package control;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.UserDTO;
import business.UserManager;

public class AddUserServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}  	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	} 
	
	/**
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserManager um = new UserManager();
		UserDTO user = new UserDTO();
		System.out.println("in");
         Map<String, String> errors = new HashMap<String, String>();
		
		String origin = request.getParameter("username");
		String origin2 = request.getParameter("userpassword");
		String origin3 = request.getParameter("email");
		String origin4 = request.getParameter("phone");
		String origin5 = request.getParameter("managerid");
		if (origin.isEmpty()) {
			errors.put("origin", "Enter User Name");
		}else
		{
			errors.remove("origin");
		}
		if (origin2.isEmpty()||origin2.length()<6) {
			errors.put("origin2", "Enter validPassword, more than 6 digits please");
		}else
		{
			errors.remove("origin2");
		}
		if ((origin3.isEmpty())||!(origin3.matches("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@"+
	              "[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$" ))) {
				errors.put("origin3", "Enter only Email format eg. abc@gmail.com");
			}else
			{
				errors.remove("origin3");
			}
		if ((origin4.isEmpty()) || !(origin4.matches("\\(?[6][5]\\)?-[689]\\d{7}"))) {
			errors.put("origin4", "Phone number formateg.65-60000000 or 65-80000000 or 65-9....");
		}else
		{
			errors.remove("origin4");
		}
		if ((origin4.isEmpty())||!(origin5.matches("\\d+"))) {
			errors.put("origin5", "Enter only Number");
		}else
		{
			errors.remove("origin5");
		}
		
			if (errors.isEmpty()) {
			user.setUserName(request.getParameter("username"));
			user.setUserPW(request.getParameter("userpassword"));
			user.setEmail((request.getParameter("email")));
			user.setPhone(request.getParameter("phone"));
			user.setRole(request.getParameter("role"));
			user.setManagerID(Integer.parseInt(request.getParameter("managerid")));
			um.insertUser(user);
			
		
		ArrayList<UserDTO> data = um.findAllUsers();
		request.setAttribute("users", data);
		RequestDispatcher rd = request.getRequestDispatcher("/ManageUser.jsp");
		try {
			rd.forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
			else {
				request.setAttribute("errors", errors);
				request.setAttribute("type", request.getParameter("type"));
		        request.getRequestDispatcher("AddUser.jsp").forward(request, response);
			
				}
		
	}
}
